package com.huazai.out_school_manager.Servlet;

import com.huazai.out_school_manager.Service.UserService;
import com.huazai.out_school_manager.Service.realization.UserServiceReal;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "login", value = "/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userid = request.getParameter("userid");
        String password = request.getParameter("password");
        String field = request.getParameter("field");

//        System.out.println("userid = " + userid);
//        System.out.println("password = " + password);
//        System.out.println("field= " + field);

        UserService userService = new UserServiceReal();
        String mes = userService.doLogin(userid, password, field, request);

        /**
         * 个人认为这里可以改为session的形式来传递数据，以此来将网页直接重定向到/admin或者/user的网页界面
         */
        if (mes.equals("登录成功")) {
            if ( field.equals("user") ){
                request.getRequestDispatcher("/user").forward(request, response);
            }
            else {
                request.getRequestDispatcher("/admin").forward(request, response);
            }
            //进行转发请求
        }
        else {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter pw = response.getWriter();
            pw.print("<h1 align=\"center\">" + mes + "</h1>");
            pw.print("<a align=\"center\" href=\"login.html\">点这里重新登录</a>");
        }

    }

}
